package com.sunmouse.toolkit;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.ColorRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;

import com.sunmouse.toollib.activity.OnBindInit;
import com.sunmouse.toollib.activity.OnHandleMessageListener;
import com.sunmouse.toollib.utils.PersistentUtils;

import java.util.ArrayList;

/**
 * Created by leiyun on 2018/4/17.
 */
public class BaseActivity extends AppCompatActivity implements OnBindInit, View.OnClickListener, OnHandleMessageListener {

    protected final Handler mHandler = new MessageHandler();
    protected FragmentManager mManager;
    protected ArrayList<Fragment> fragments;
    protected int index;

    private boolean isInitialized;
    protected PersistentUtils mPersistentUtils;

    protected Intent mIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        BaseApp.getInstance().addAct(getActivity());//use for exit

        isInitialized = false;

        mManager = getSupportFragmentManager();
        index = 0;

        if (mPersistentUtils == null) {
            mPersistentUtils = PersistentUtils.getInstance(this, "tool_kit");
        }
    }


    @Override
    protected void onStart() {
        super.onStart();
        if (!isInitialized) {
            isInitialized = true;
            initNav();
            init();
        }
    }


    public Activity getActivity() {
        return this;
    }

    /**
     * hide  status bar
     */
    protected void hideStatusBar() {
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    /**
     * hide action bar
     */
    protected void hideActionbar() {
        ActionBar bar = this.getSupportActionBar();
        if (bar != null)
            bar.hide();
    }

    /**
     * show content behind status bar
     * <p>
     * >attention:if you`ve defined in a style file ,you should analysis the differences
     */
    protected void showBehindStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            getWindow().setStatusBarColor(getResources().getColor(R.color.transparent));
            decorView.setFitsSystemWindows(true);
        }
    }

    /**
     * set status bar color
     */
    protected void setStatusBarColor(@ColorRes int colorId) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            getWindow().setStatusBarColor(getResources().getColor(R.color.transparent));
    }

    /**
     * check showing status
     */
    protected boolean isActionbarShowing() {
        ActionBar bar = this.getSupportActionBar();
        if (bar != null) return bar.isShowing();
        return false;
    }

    /**
     * show actionbar
     */
    protected void showActionbar(boolean showing) {
        ActionBar bar = this.getSupportActionBar();
        if (bar != null) {
            if (!bar.isShowing() && showing) bar.show();
            bar.hide();
        }
    }

    @Override
    public void init() {

    }

    @Override
    public void initNav() {

    }

    @Override
    public void initDialog() {

    }

    @Override
    public void loadingData(int page) {

    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onHandleMessage(Message msg) {

    }


    /**
     * msg handler
     */
    private class MessageHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
            onHandleMessage(msg);
        }
    }
}
